package com.example.testuploadfile.download;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.FileOutputStream;
import java.io.IOException;

public class DownloadWordFile {

    public static void main(String[] args) {
        // 创建一个新的Word文档
        XWPFDocument document = new XWPFDocument();

        // 创建一个段落
        StringBuilder content = new StringBuilder();
        content.append("1\n00:00:01.90 --> 00:00:03.470\n人们都说\n");
        content.append("2\n00:00:03.470 --> 00:00:06.870\n但是其实\n");
        content.append("3\n00:00:06.870 --> 00:00:08.230\n确认很难\n");


        // 设置文件路径
        String filePath = "C:\\Users\\84186\\Desktop\\example.docx";


        String[] lines = content.toString().split("\n");  // 按行分割

        for (String line : lines) {
            XWPFParagraph paragraph = document.createParagraph();  // 每行字幕一个段落
            XWPFRun run = paragraph.createRun();
            run.setText(line);  // 添加文本内容
        }


        // 将文档写入文件
        try (FileOutputStream out = new FileOutputStream(filePath)) {
            document.write(out);
            System.out.println("Word文件已成功下载到本地路径: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}